/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.Articolo;
import ejb.BusinessLogic;
import ejb.Commento;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Administrator
 */
public class CreaProdotto extends HttpServlet {

    @EJB
    private BusinessLogic businessLogic;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String id = request.getParameter("valore");
        List<Articolo> lart = businessLogic.listaArticoliOrdinati();
        HttpSession s = request.getSession();
        String utente = (String) s.getAttribute("username");
        int size = lart.size();
        int i = 0;
        int quant = 0;
        boolean flag = false;
        boolean flagTaglia = false;
        String artI = "";
        int artID = 0;
        String artModello = "";
        String artMarca = "";
        String taglia = "<select name=\"taglia\" ONCHANGE='getQuantita(this.options[this.selectedIndex].value)'>\n";
        while (i < size && !flag) {
            quant = lart.get(i).getQuantita();
            if (quant > 0) {
                if (!flagTaglia) {
                    artI = lart.get(i).getId().toString();
                    if (artI.equals(id)) {
                        artID = i;
                        artModello = lart.get(i).getModello();
                        artMarca = lart.get(i).getMarca();
                        taglia = taglia + "<option value=" + lart.get(i).getId() + ">" + lart.get(i).getTaglia() + "</option>\n";
                        flagTaglia = true;
                    }
                } else {
                    if ((artModello.equals(lart.get(i).getModello())) && (artMarca.equals(lart.get(i).getMarca()))) {
                        taglia = taglia + "<option value=" + lart.get(i).getId() + ">" + lart.get(i).getTaglia() + "</option>\n";
                    } else {
                        flag = true;
                    }
                }
            }
            i++;
        }
        taglia = taglia + "</select>\n";
        out.println("<div class='articolo'>");
        if (flagTaglia) {
            String foto = lart.get(artID).getFoto();
            out.println("<div class='intestazione'> <img src='images/" + foto + "' alt='' width='30%' height='45%' class='left' />");
            out.println("<div class='marca_prod'>" + lart.get(artID).getMarca() + " "
                    + lart.get(artID).getModello() + "</div>"
                    + "<div class='categoria_prod'>Categoria : " + lart.get(artID).getTipo() + "</div>"
                    + "<div class='taglia_prod'>Taglia : " + taglia + "</div>"
                    + "Prezzo : <div class='prezzo_prod'>" + lart.get(artID).getPrezzo_vendita() + " €</div>"
                    + "<div id='quant_prod2'>Quantit&agrave; : " + lart.get(artID).getQuantita() + "</div>");
            if (utente != null) {
                out.println("<form name='prodotto' action='Controller' method='post'>");
                out.println("<input type='submit' value='Aggiungi al carrello' name='add' />"
                        + "<input type='hidden' name='articolo' value='" + lart.get(artID).getId() + "'/>");
                out.println("<select name='quantita_taglia'>");
                for(int j = 1; j <= lart.get(artID).getQuantita(); j++)
                    out.println("<option value=" + j + ">" + j + "</option>");
                out.println("</select>");
                out.println("</form>");
            }
            out.println("</div><div class='descr'>Descrizione : " + lart.get(artID).getDescrizione() + "</div>"
                    + "<div class='fb-like' data-href='http://localhost:8080/LocalitaSciistica-war/prodotto.jsp?valore=" + artID + "' data-send='true' data-width='450' data-show-faces='true'></div>"
                    + "<label id='label_mex'>Scrivi un commento</label>");
            if (utente != null) {
                out.println("<form id='login' method='post' action='Controller'>"
                        + "<textarea name='messaggio' id='messaggio' cols='30' rows='10'></textarea>"
                        + "<input type='hidden' name='id_prodotto' value='" + lart.get(artID).getId() + "'/>"
                        + "<input id='inputcommento' type='submit' name='inputcommento' value='Aggiungi Commento' /></form>");//chiusura div intestazione
            } else {
                out.println("<div><label id='label_avviso'>Attenzione! Per poter commentare devi essere registrato!</label></div>");
            }
            List<Commento> c = businessLogic.listaCommentiOrdinati(lart.get(artID).getId());
            i = 0;
            String u = "";
            out.println("<div id='commenti'><h3>Commenti degli utenti</h3>");
            while (i < c.size()) {
                u = c.get(i).getUser_id();
                out.println("<div class='commento'><div class='user_commento'>" + u + " ha postato il " + c.get(i).getData() + "</div>");
                out.println("<div class='mess_commento'>" + c.get(i).getCommento_prod() + "</div></div>");
                i++;
            }
            if (i == 0) {
                out.println("<div class='mess_commento'>Non ci sono commenti</div>");
            }
            out.println("</div>");
        } else {
            out.println("<div>Attenzione! Prodotto inesistente!</div>");
        }
        out.println("<div><a id='link_negozio' href='negozio.jsp'>Torna al negozio</a></div></div>"); //chiusura div articolo
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
